﻿using Lamex.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mNC.Common
{
   public class GetSerialNumber
    {
        /// <summary>
        /// 读取数据库批量加工二维码的流水号
        /// </summary>
        /// <param name="OrderNumber">一维码</param>
        /// <returns></returns>
        public static int GetSerialNo(string OrderNumber)
        {
            int result = 1;
            string sql = @" SELECT RIGHT(BARCODE_NO,3) as Numbers FROM [LamexMESManager].[dbo].[NC_PN_AL_Manufacture_Process_Record]
         where BARCODE_NO like '" + OrderNumber + "_ZPL%' " +
         "order by BARCODE_NO DESC";
            DataTable dt = BaseBuildSql.GetDataTableBySql(sql);
            if(dt.Rows.Count>0)
            {
                result += Convert.ToInt32(dt.Rows[0]["Numbers"].ToString());
            }         
            return result;
        }
        /// <summary>
        /// 读取数据库余量加工二维码的流水号
        /// </summary>
        /// <param name="OrderNumber"></param>
        /// <returns></returns>
        public static int GetSerialNo_YL(string OrderNumber)
        {
            int result = 1;
            string sql = @" SELECT RIGHT(BARCODE_NO,3) as Numbers FROM [LamexMESManager].[dbo].[NC_PN_AL_Manufacture_Process_Record]
         where BARCODE_NO like '" + OrderNumber + "_ZWS%' " +
         "order by BARCODE_NO DESC";
            DataTable dt = BaseBuildSql.GetDataTableBySql(sql);
            if (dt.Rows.Count > 0)
            {
                result += Convert.ToInt32(dt.Rows[0]["Numbers"].ToString());
            }
            return result;
        }

        /// <summary>
        /// 读取数据库批量加工二维码的流水号-自定义
        /// </summary>
        /// <param name="OrderNumber">一维码</param>
        /// <returns></returns>
        public static int GetSerialNo_Custom(string OrderNumber)
        {
            int result = 1;
            string sql = @" SELECT RIGHT(BARCODE_NO,3) as Numbers FROM [LamexMESManager].[dbo].[NC_PN_AL_Manufacture_Process_Record]
         where BARCODE_NO like '" + OrderNumber + "_BPL%' " +
         "order by BARCODE_NO DESC";
            DataTable dt = BaseBuildSql.GetDataTableBySql(sql);
            if (dt.Rows.Count > 0)
            {
                result += Convert.ToInt32(dt.Rows[0]["Numbers"].ToString());
            }
            return result;
        }
        /// <summary>
        /// 读取数据库余量加工二维码的流水号-自定义
        /// </summary>
        /// <param name="OrderNumber"></param>
        /// <returns></returns>
        public static int GetSerialNo_YL_Custom(string OrderNumber)
        {
            int result = 1;
            string sql = @" SELECT RIGHT(BARCODE_NO,3) as Numbers FROM [LamexMESManager].[dbo].[NC_PN_AL_Manufacture_Process_Record]
         where BARCODE_NO like '" + OrderNumber + "_BWS%' " +
         "order by BARCODE_NO DESC";
            DataTable dt = BaseBuildSql.GetDataTableBySql(sql);
            if (dt.Rows.Count > 0)
            {
                result += Convert.ToInt32(dt.Rows[0]["Numbers"].ToString());
            }
            return result;
        }
        public static string GetSerialNo_IssueRecord()
        {
            string sql = @" SELECT TOP 1 ISSUE_NO FROM [LamexMESManager].[dbo].[NC_Convert_IssueRecord] ORDER BY ISSUE_NO DESC";
            DataTable dt = BaseBuildSql.GetDataTableBySql(sql);
            if (dt.Rows.Count > 0)
                return dt.Rows[0]["ISSUE_NO"].ToString();
            else
                return "";
        }
    }
}
